[Previous] [Next]

Chapter 2

Introduction to Forms

Forms are the first Microsoft Visual Basic objects you get acquainted with. Although you can write useful programs with only rudimentary user interfaces—command-line driven utilities, for example—most Visual Basic applications include one or more forms, and so you need to be familiar with their properties and features.

Despite their different natures, forms and controls have one important thing in common: All of them are Visual Basic objects, and as such they expose properties, react to methods, and raise events. In this sense, Visual Basic is said to be an object-based programming language because the developer's job consists of reading and modifying objects' properties, calling their methods, and responding to their events. Moreover, Visual Basic can be considered a visual programming environment in that the appearance of such objects can be defined by means of interactive tools at design time and without writing any code.

Forms and controls expose dozens of properties, and when you explore them in the Object Browser you might legitimately wonder how you can learn the meaning of all of them. It takes some time until you realize that there are a few recurring patterns and that most of the properties are shared among forms and among most types of controls. In the end, properties that are peculiar to form objects or to a given class of controls are relatively few.

This consideration led me to structure this and the following chapter in a somewhat unusual way. Most language manuals introduce forms first, describe each intrinsic control class and illustrate its features, passing from one control to the next, and so on. In my opinion, this approach forces you to study each particular object as if it were a separate case. Such fragmentation of information makes it difficult to get the big picture, and in the end it makes for a steeper leaning curve. Worse, this mnemonic effort doesn't help much in understanding how things actually work behind the scenes. For example, why do some controls expose a TabIndex property but not a TabStop property? Why do some controls support the hWnd property, while others do not?

After some thinking, I decided to depart from the typical control-by-control description and focus instead on the many properties, methods, and events that forms and most intrinsic controls have in common. The features and peculiarities of forms are covered only later in this chapter, and Chapter 3 is entirely devoted to Visual Basic's intrinsic controls. This means that you won't see complete programming examples until the second half of this chapter, even though I use some shorter pieces of code that explain how a property can be used or how you usually react to the events that are shared by most types of controls. After all, when you're working in the Visual Basic environment, the complete list of all the properties, methods, and events supported by each object is always just one keystroke away: Just press F2 to display the Object Browser or F1 to get more complete and descriptive help. You don't want to read the same information here, do you?

I have another reason for explaining common properties in one section. In its six versions, Visual Basic has undergone many important changes: Each version has added new features and, consequently, forms and controls have acquired more and more new properties, methods, and events. Backward compatibility has always been a primary goal in Microsoft's plans, however, and old features are still supported. In fact, you can often load a Visual Basic 3 project in the Visual Basic 6 environment and run it without changing a single line of code. (The most notable exceptions are code that refers to external libraries and controls that access databases.) Backward compatibility has its drawbacks, though, the first of which is the ever-growing list of properties, methods, and events. For example, there are duplicated sets of properties that have to do with drag-and-drop, and there are two distinct ways to set font attributes. The result is that most beginning programmers are confused, and more seasoned developers tend to continue to use the old (and often inefficient) features because they don't want to learn a new syntax. I hope that the following descriptions of common properties, methods, and events can contribute to making things clearer to both kinds of readers.